package business_opportunity

import (
	"git.mycaigou.com/gfyx/micro-gfyx-api/http_server/controller"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/common/errcode"
	dto "git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/dto/business_opportunity"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/utils"
	"git.mycaigou.com/gfyx/micro-gfyx-api/service/business_opportunity/project"
	"git.myscrm.cn/golang/stark/v4"
	"github.com/gin-gonic/gin"
)

type ProjectController struct {
	controller.BaseController
	projectService project.ProjectServiceIface
}

// NewProjectController 找项目
func NewProjectController(
	projectService project.ProjectServiceIface,
) *ProjectController {
	return &ProjectController{
		projectService: projectService,
	}
}

// RegisterRouter
// 注册路由
func (c *ProjectController) RegisterRouter(g *gin.RouterGroup) {
	// 搜索项目
	g.POST("project/search-project", c.searchProjectList)
	// 搜索项目
	g.GET("project/fuzzy-search-project", c.fuzzySearchProject)
	// 项目详情
	g.POST("project/detail", c.projectDetail)
	// 项目详情 - 拿地列表
	g.POST("project/detail-land", c.projectDetailLand)
	// 项目详情 - 在建列表
	g.POST("project/detail-build", c.projectDetailBuild)
	// 项目详情 - 开盘列表
	g.POST("project/detail-sale", c.projectDetailSale)
	// 项目详情 - 招中标列表
	g.POST("project/detail-bid", c.projectDetailBid)

	// 拿地详情
	g.POST("project/taken-land-detail", c.takenLandDetail)
	// 开盘列表
	g.POST("project/sale-list", c.saleList)
	// 开盘列表-导出
	g.POST("project/export-sale-list", c.exportSaleList)
	// 开盘详情
	g.POST("project/sale-detail", c.saleDetail)
	// 新房销售
	g.POST("project/new-house", c.newHouse)
	// 获取项目主体
	g.GET("project/get-top-project", c.getTopProject)
}

func (c *ProjectController) searchProjectList(ctx *gin.Context) {
	req := &dto.SearchProjectRequest{}
	if err := ctx.ShouldBind(&req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}

	res, err := c.projectService.SearchProject(ctx, req)
	if err != nil {
		stark.Logger.Errorf(ctx, "project/search-project err:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}
	c.Success(ctx, res)
}

func (c *ProjectController) fuzzySearchProject(ctx *gin.Context) {
	req := &dto.FuzzySearchProjectRequest{}
	if err := ctx.ShouldBind(&req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}

	res, err := c.projectService.FuzzySearchProject(ctx, req)
	if err != nil {
		stark.Logger.Errorf(ctx, "project/fuzzy-search-project err:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}
	c.Success(ctx, res)
}

func (c *ProjectController) projectDetail(ctx *gin.Context) {
	req := &dto.ProjectDetailRequest{}
	if err := ctx.ShouldBind(&req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}

	res, err := c.projectService.ProjectDetail(ctx, req)
	if err != nil {
		stark.Logger.Errorf(ctx, "project/detail err:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}
	c.Success(ctx, res)
}

func (c *ProjectController) projectDetailLand(ctx *gin.Context) {
	req := &dto.ProjectDetailLandRequest{}
	if err := ctx.ShouldBind(&req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}

	res, err := c.projectService.ProjectDetailLand(ctx, req)
	if err != nil {
		stark.Logger.Errorf(ctx, "project/detail-land err:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}
	c.Success(ctx, res)
}

func (c *ProjectController) projectDetailBuild(ctx *gin.Context) {
	req := &dto.ProjectDetailBuildRequest{}
	if err := ctx.ShouldBind(&req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}

	res, err := c.projectService.ProjectDetailBuild(ctx, req)
	if err != nil {
		stark.Logger.Errorf(ctx, "project/detail-build err:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}
	c.Success(ctx, res)
}

func (c *ProjectController) projectDetailSale(ctx *gin.Context) {
	req := &dto.ProjectDetailSaleRequest{}
	if err := ctx.ShouldBind(&req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}

	res, err := c.projectService.ProjectDetailSale(ctx, req)
	if err != nil {
		stark.Logger.Errorf(ctx, "project/detail-sale err:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}
	c.Success(ctx, res)
}

func (c *ProjectController) projectDetailBid(ctx *gin.Context) {
	req := &dto.ProjectDetailBidRequest{}
	if err := ctx.ShouldBind(&req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}

	res, err := c.projectService.ProjectDetailBid(ctx, req)
	if err != nil {
		stark.Logger.Errorf(ctx, "project/detail-bid err:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}
	c.Success(ctx, res)
}

func (c *ProjectController) takenLandDetail(ctx *gin.Context) {
	req := &dto.TakenLandDetailRequest{}
	if err := ctx.ShouldBind(&req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}

	res, err := c.projectService.TakenLandDetail(ctx, req)
	if err != nil {
		stark.Logger.Errorf(ctx, "project/taken-land-detail err:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}
	c.Success(ctx, res)
}
func (c *ProjectController) saleList(ctx *gin.Context) {
	req := &dto.SaleListRequest{}
	if err := ctx.ShouldBind(&req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}

	res, err := c.projectService.SaleList(ctx, req)
	if err != nil {
		stark.Logger.Errorf(ctx, "project/sale-list err:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}
	c.Success(ctx, res)
}

func (c *ProjectController) exportSaleList(ctx *gin.Context) {
	req := &dto.ExportSaleListRequest{}
	if err := ctx.ShouldBind(&req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}

	res, err := c.projectService.ExportSaleList(ctx, req)
	if err != nil {
		stark.Logger.Errorf(ctx, "project/export-sale-list err:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, err.Error())
		return
	}
	c.Success(ctx, res)
}

func (c *ProjectController) saleDetail(ctx *gin.Context) {
	req := &dto.SaleDetailRequest{}
	if err := ctx.ShouldBind(&req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}

	res, err := c.projectService.SaleDetail(ctx, req)
	if err != nil {
		stark.Logger.Errorf(ctx, "project/sale-detail err:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}
	c.Success(ctx, res)
}

func (c *ProjectController) newHouse(ctx *gin.Context) {
	req := &dto.NewHouseRequest{}
	if err := ctx.ShouldBind(&req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}

	res, err := c.projectService.NewHouse(ctx, req)
	if err != nil {
		stark.Logger.Errorf(ctx, "project/new-house err:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}
	c.Success(ctx, res)
}

func (c *ProjectController) getTopProject(ctx *gin.Context) {
	req := &dto.GetTopProjectRequest{}
	if err := ctx.ShouldBind(&req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}

	res, err := c.projectService.GetTopProject(ctx, req)
	if err != nil {
		stark.Logger.Errorf(ctx, "project/get-top-project err:%s", err.Error())
		c.Fail(ctx, errcode.COMMON_DATA_NOT_EXIST, "接口返回异常")
		return
	}
	c.Success(ctx, res)
}
